const express = require('express')
const router = express.Router()

const {
    getOrderLists,
    pageGetOrderLists,
    addOrder,
    ToBePaid,
    WaitingList,
    delivery,
    getPaidOrderLists,
    pageGetPaidOrderLists
} = require('../db/model/orderModel/orderModel')


/**
 * 分页查询订单信息
 * post请求
 * 参数page，pageSize
 * 
 *  */ 
router.post('/pageGetOrderLists',(req,res) => {
    const { page,pageSize } = req.body
    const result = pageGetOrderLists(page,pageSize)
    const result1 = getOrderLists()
    Promise.all([result,result1]).then(val => {
        if(val.length!==0){
            res.send({code:200,msg:'查询成功',orderLists:val[0],count:val[1].length})
        }else{
            res.send({code:-1,msg:'查询失败',orderLists:val[0],count:val[1].length})
        }
    })
})


/**
 * 获取不同类型的订单
 * post请求
 * 参数：status page pageSize
 * 
 */
router.post('/pageGetPaidOrderLists',(req,res) =>{
    const { status,page,pageSize } = req.body
    const result = pageGetPaidOrderLists(status,page,pageSize)
    const result1 = getPaidOrderLists(status)
    Promise.all([result,result1]).then(val => {
        if(val.length!==0){
            res.send({code:200,msg:'查询成功',orderLists:val[0],count:val[1].length})
        }else{
            res.send({code:-1,msg:'查询失败',orderLists:val[0],count:val[1].length})
        }
    })
})

/**
 * 增加订单
 * post请求
 * 参数：orderTime,serverTime,status,customerId,staffId,payment,address,phone,remark
 */
router.post('/addOrder',(req,res) => {
    const { orderTime,serverTime,status,customerId,staffId,payment,address,phone,remark } = req.body
    const result = addOrder(orderTime,serverTime,status,customerId,staffId,payment,address,phone,remark)
    return result.then(val => {
        if(val){
            res.send({code:200,msg:'添加成功'})
        }else{
            res.send({code:-1,msg:'添加失败'})
        }
    })
})

/**
 * 待下单订单转换为待派单订单
 * post请求
 * 
 */
router.post('/toBePaid',(req,res) => {
    const { ToBePaidOrderNum,orderTime } = req.body
    const result = ToBePaid(ToBePaidOrderNum,orderTime)
    return result.then(val => {
        if(val){
            res.send({code:200,msg:'修改成功'})
        }else{
            res.send({code:-1,msg:'修改失败'})
        }
    })
})


/**
 * 待派单订单转换为派送中订单
 * post请求
 * 
 */
router.post('/WaitingList',(req,res) => {
    const { ToBePaidOrderNum,serverTime } = req.body
    const result = WaitingList(ToBePaidOrderNum,serverTime)
    return result.then(val => {
        if(val){
            res.send({code:200,msg:'修改成功'})
        }else{
            res.send({code:-1,msg:'修改失败'})
        }
    })
})


/**
 * 派送中订单转换为已完成订单
 * post请求
 * 
 */
router.post('/delivery',(req,res) => {
    const { ToBePaidOrderNum,serverTime } = req.body
    const result = delivery(ToBePaidOrderNum,serverTime)
    return result.then(val => {
        if(val){
            res.send({code:200,msg:'修改成功'})
        }else{
            res.send({code:-1,msg:'修改失败'})
        }
    })
})






module.exports = router